
Page({

  /**
   * 页面的初始数据
   */
  data: {
    titleHeight:"0px",
    noCentent:false,
    un:undefined,
    cookie:"",
    https: "https://csb2c.fenxiangqu.cn/",
    imgerror: 'nooo',
    wh: null,
    types:{
      chils:[]
    },
    typecheck:false,
    index:0,
    sort:{
      chils: ["综合排序", "销量排序", "好评排序", "价格从低到高","价格从高到低"],
      val: ["1","2","3","4","5"]
    },
    sortcheck: false,
    sindex: 0,
    fit:{
      chils: ["全部", "男性", "女性", "中老年", "儿童","孕妇"],
      val: ["", "男性", "女性", "中老年", "儿童", "孕妇"]
    },
    fitcheck:false,
    findex: 0,
    px:1,
    sy:"",
    n:10,
    sou:"",
    guid:"",
    page:1,
    tier:null,
    mate:[],
    iscfy:true,
    scroll:0   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 10000
    })
    var a = wx.getStorageSync('cookie') || [];
    this.setData({
      cookie: a
    }) 
    try {
      var res = wx.getSystemInfoSync();
      this.setData({
        wh: res.windowHeight + 'px'
      })

    } catch (e) {
      // Do something when catch error
      console.log(e)
    }
    var self = this;
    this.setData({
      px:e.px,
      sy:e.sy,
      n:this.data.n,
      page:e.page,
      tier:e.tier,
      guid:e.guid,
      sou:e.sou
    })
    var obj = this.data;
    this.searchOverList(obj,function(res){
      if (res.data) {
          self.setData({
            mate: res.data,
            onCentent:false
          })
      }else{
        self.setData({
          mate: [],
          noCentent: true
        })
      }
      if (res.errMsg){
        console.log(res.errMsg)
      }
    })
    wx.request({
      url: "https://csb2c.fenxiangqu.cn/xcx_zb_000009x",
      data: {
        guid: self.data.guid,
        tier: self.data.tier
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "LID=" + this.data.cookie
      },
      method: "POST",
      success: res => {
        let s = res.data;
        const html = 'types.chils';
        s.unshift({
          "text":"全部",
          "flguid": res.data[0].parentID,
          "tier": res.data[0].tier-1
        })
        if(res.data){
          self.setData({
            [html]: s
          })                           
        }
        wx.hideToast()
      },
      fail: res => {
        console.log(res)
        wx.hideToast()
      }
    })
    
  },
  imagError:function(e){
    const m = e.currentTarget.dataset.index;
    const html = "mate[0][" + m + "].sptm";
    this.setData({
      [html]: this.data.imgerror
    })
  },
  bindPickerchils:function(e){
    const self = this;
    const val = e.detail.value;
    this.setData({
      px: this.data.px,
      sy: this.data.sy,
      n: this.data.n,
      page: "1",
      tier: this.data.types.chils[val].tier,
      guid: this.data.types.chils[val].flguid,
      sou:""
    })
    this.searchOverList(self.data,function(e){
      console.log(e)
      if (e.data) {
        console.log(e.data)
        self.setData({
          mate: e.data,
          typecheck:true,
          index:val,
          onCentent:false
        })
      }else{
        self.setData({
          mate: [],
          typecheck: true,
          index: val,
          noCentent: true
        })
      }
      if (e.errMsg) {
        console.log(e.errMsg)
      }
    })  
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  searchOverList: function (obj,callback){
    wx.request({
      url: "https://csb2c.fenxiangqu.cn/xcx_wx_searchOverList",
      data: {
        sou: obj.sou,
        guid: obj.guid,
        tier: obj.tier,
        px: obj.px,
        type: obj.sy,
        page: obj.page,
        count: obj.n
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "LID=" + obj.cookie
      },
      method: "POST",
      success: res => {
        console.log(res)
        if(res){
          callback && callback(res)
        }
        
      },
      fail: res => {
        if (res) {
          callback && callback(res)
        }
      }
    })
  },
  bindPickersort:function(e){
    const self = this, val = e.detail.value;
    this.setData({
      px: this.data.sort.val[val],
      sy: this.data.sy,
      n: this.data.n,
      page: "1",
      tier: this.data.tier,
      guid: this.data.guid,
      sou: ""
    })
    this.searchOverList(self.data, function (e) {
      if (e.data) {
        self.setData({
          mate: e.data,
          sortcheck: true,
          sindex:val,
          noCentent:false
        })
      }else{
        self.setData({
          mate: [],
          sortcheck: true,
          sindex: val,
          noCentent: true
        })
      }
      if (e.errMsg) {
        console.log(e.errMsg)
      }
    })
  },
  bindPickerChange:function(e){
    const self = this, val = e.detail.value;
    this.setData({
      px: this.data.px,
      sy: this.data.fit.val[val],
      n: this.data.n,
      page: "1",
      tier: this.data.tier,
      guid: this.data.guid,
      sou: ""
    })
  this.searchOverList(self.data, function (e) {
      if (e.data) {
        self.setData({
          mate: e.data,
          fitcheck: true,
          findex: val,
          noCentent:false
        })
      }else{
        self.setData({
          mate: [],
          fitcheck: true,
          findex: val,
          noCentent:true
        })
      }
      if (e.errMsg) {
        console.log(e.errMsg)
      }
    })
  },
  eventlower:function(e){
    var self = this;
    wx.request({
      url: "https://csb2c.fenxiangqu.cn/xcx_wx_searchOverList",
      data: {
        sou: self.data.sou,
        guid: self.data.guid,
        tier: self.data.tier,
        px: self.data.px,
        type: self.data.sy,
        page: parseInt(self.data.page)+1,
        count: self.data.n
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "LID=" + self.data.cookie
      },
      method: "POST",
      success: res => {
        if(res.data){
          for(var i=0;i<res.data.length;i++){
            if(res.data[0][i]){
              const html = "mate[0]",
                    html2 = "mate[2]",
                    html3 = "mate[3]",
                    html4 = "mate[4]";
              self.setData({
                [html]: self.data.mate[0].concat(res.data[0][i]),
                [html2]: self.data.mate[2].concat(res.data[2][i]),
                [html3]: self.data.mate[3].concat(res.data[3][i]),
                [html4]: self.data.mate[4].concat(res.data[4][i])
              })
            }
            
          }          
          self.setData({
            page: parseInt(self.data.page) + 1
          })          
        }
      },
      fail: res => {
        wx.showToast({
          title: '网络故障！',
          icon: 'success',
          duration: 1000
        })
      }
    })
  },
  onReady: function () {

  },
  navgwc:function(e){
    console.log(e)
    wx.switchTab({
      url:"../goods/goods",
      fail:function(e){
        console.log(e)
      }
    })
  },
  commpry:function(e){
    var self = this;
    const id = e.currentTarget.dataset.id;
    wx.request({
      url: "https://csb2c.fenxiangqu.cn/xcx_commodity_iscfy",
      data: {
        spid: id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "LID=" + self.data.cookie
      },
      method: "POST",
      success: function(res) {
        if(res.data){
          
            if(res.data.iscfy){
              var iscfy = res.data.iscfy;
            }else{
              var iscfy = false;
            }
            if(iscfy){
              wx.navigateTo({
                url: '../cfcommp/cfcommp?id=' + id + '&cfy=' + iscfy
              })
            }else{
              wx.navigateTo({
                url: '../commp/commp?id=' + id + '&cfy=' + iscfy
              })
            }
        }else{
          wx.showToast({
            title: '抱歉！商品不存在或已下架!',
            icon: 'success',
            duration: 1000
          })
        }
      },
      fail: function(res) {
        wx.showToast({
          title: '网络故障！',
          icon: 'success',
          duration: 1000
        })
      }
    })
    
  },
  gwc: function (e) {
    var self = this;
    const id = e.currentTarget.dataset.id;
      wx.request({
        url: "https://csb2c.fenxiangqu.cn/xcx_addgwc_wx",
        data: {
          xl: 1,
          id: id
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Cookie': "LID=" + self.data.cookie
        },
        method: "POST",
        success: res => {
          if (res.data == "login") {
            wx.showModal({
              title: '登录提示',
              content: '您未登录，是否前往登录？',
              success: function (res) {
                if (res.confirm) {
                  wx.switchTab({
                    url: '../center/center'
                  })
                } else if (res.cancel) {
                  wx.switchTab({
                    url: '../index/index'
                  })
                }
              }
            })
          } else {
            console.log(res.data)
            if (res.data.error != undefined) {

              console.log('11111')
              wx.showToast({
                title: '网络故障！',
                icon: 'success',
                duration: 1000
              })
            } else {
              wx.showToast({
                title: res.data.ret,
                icon: 'success',
                duration: 1000
              })
              wx.request({
                url: "https://csb2c.fenxiangqu.cn/xcx_50010_wx",
                data: {
                  a: ""
                },
                header: {
                  'content-type': 'application/x-www-form-urlencoded',
                  'Cookie': "LID=" + self.data.cookie
                },
                method: "POST",
                success: res => {
                  console.log(res)
                },
                fail: res => {
                  wx.showToast({
                    title: '网络故障！',
                    icon: 'success',
                    duration: 1000
                  })
                }

              })
            }
          }
        },
        fail: res => {
          wx.showToast({
            title: '网络故障！',
            icon: 'success',
            duration: 1000
          })
        }

      })
    

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.eventlower()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})